unit smClassPlayer;

interface

uses
  Lua, smExceptions;

const
  luaCLASS_PLAYER: AnsiString = 'Player';

  procedure OpenClass(const Lua: lua_State);

implementation

uses
  smLuaUtils, smPlayer;

function _PlayerNew(L: lua_State): Integer; cdecl; forward;
function _PlayerLoad(L: lua_State): Integer; cdecl; forward;
function _PlayerClear(L: lua_State): Integer; cdecl; forward;

const
  PLAYER_CLASS: array [0..3] of luaL_reg = (
    (name: 'New'; func: _PlayerNew),
    (name: 'Load'; func: _PlayerLoad),
    (name: 'Clear'; func: _PlayerClear),
    (name: nil; func: nil)
  );

function _PlayerNew;
begin
  Player.New();
  Result := 0;
end;

function _PlayerLoad;
begin
  try
    if lua_gettop(L) > 1 then
      if lua_isstring(L, 1) then
        Player.Load(String(lua_tostring(L, 1)))
      else
        raise ELuaArgumentCheck.Create(L, 'not a valid save file name')
    else
      Player.New();
  except
    // do nothing...
  end;
  Result := 0;
end;

function _PlayerClear;
begin
  Player.Clear();
  Result := 0;
end;

procedure OpenClass;
begin
  SetClass(luaCLASS_PLAYER, @PLAYER_CLASS[0]);
end;

end.
